//
//  OffineDownloadController.swift
//  todaySwift
//
//  Created by ChokWah on 2018/3/20.
//  Copyright © 2018年 ChokWah. All rights reserved.
//

import UIKit

class OffineDownloadController: UITableViewController {

    // 标题模型数组
    fileprivate var newsTitles = [HomeNewsTitle]()
    // 标题数据表
    fileprivate let newsTitleTable = NewsTitleTable()
    override func viewDidLoad() {
        super.viewDidLoad()

        // 头部header不会滑动，注意区分
        tableView.cwc_registerCell(cell: OffineDownloadCellTableViewCell.self)
        tableView.rowHeight = 44
        tableView.separatorColor = UIColor.globalBackgroundColor()
        tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        tableView.backgroundColor = UIColor.globalBackgroundColor()
        newsTitles = newsTitleTable.selectAll()
        
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 44) )
        view.backgroundColor = UIColor.globalBackgroundColor()
        let label = UILabel(frame: CGRect(x: 20, y: 0, width: screenWidth, height: 44) )
        view.addSubview(label)
        label.text = "我的频道"
        let separatorView = UIView(frame: CGRect(x: 0, y: 43, width: screenWidth, height: 1) )
        separatorView.backgroundColor = UIColor.globalBackgroundColor()
        view.addSubview(separatorView)
        return view
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return newsTitles.count
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cwc_dequeueReusableCell(indexPath: indexPath) as OffineDownloadCellTableViewCell
        let news = newsTitles[indexPath.row]
        cell.titleLabel.text = news.name
        cell.selectedImageView.image = news.selected ?  #imageLiteral(resourceName: "air_download_option_press_20x20_") : #imageLiteral(resourceName: "air_download_option_20x20_")
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var homeNewsTitle = newsTitles[indexPath.row] // 取出模型
        homeNewsTitle.selected = !homeNewsTitle.selected
        let cell = tableView.cellForRow(at: indexPath) as! OffineDownloadCellTableViewCell
        cell.selectedImageView.image = homeNewsTitle.selected ?  #imageLiteral(resourceName: "air_download_option_press_20x20_") : #imageLiteral(resourceName: "air_download_option_20x20_")
        newsTitles[indexPath.row] = homeNewsTitle // 赋值，更新数据库
        newsTitleTable.update(homeNewsTitle)
        tableView.reloadRows(at: [indexPath], with: .none)
    }

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
